from rest_framework.generics import ListCreateAPIView
from rest_framework.permissions import IsAdminUser

from apps.meiduo_admin.utils import CurPagination
from apps.users.models import User
from apps.meiduo_admin.serializers import UsersSerializer


class UsersView(ListCreateAPIView):
    permission_classes = [IsAdminUser]
    serializer_class = UsersSerializer
    # queryset = User.objects.filter(is_staff=False, is_active=True).all().order_by('-last_login')
    pagination_class = CurPagination

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return User.objects.filter(is_staff= False, is_active=True, username=keyword)
        else:
            return User.objects.filter(is_staff= False, is_active=True).all().order_by('id')
